package com.example.wx.dailyreport.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.wx.dailyreport.pojo.Wxword;
import com.example.wx.dailyreport.service.WxwordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hulinbo
 * @since 2020-08-08
 */

@RestController
@RequestMapping("/wxword")
public class WxwordController {

    @Autowired
    private WxwordService wxwordService;

    @GetMapping("/getwordpageby_")
    public Map<String, Object> getWordInPageBy_(Wxword wxword, Integer order, @RequestParam(defaultValue = "1") int page, int limit) {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        IPage<Wxword> wxwordIPage = wxwordService.selectWordInPageBy(wxword, username, order, page, limit);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("status", 200);
        map.put("data", wxwordIPage.getRecords());
        map.put("count", wxwordIPage.getTotal());
        return map;
    }

    @PostMapping("/add")
    public Map<String, Object> insertRole(Wxword wxword) {
        Map<String, Object> map = new LinkedHashMap<>();
        if (wxwordService.save(wxword))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }


    @GetMapping("/getmyword")
    public Map<String, Object> getMyWord(@RequestParam(defaultValue = "1") int page, int limit) {
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        IPage<Wxword> wxwordIPage = wxwordService.selectMyWord(username, page, limit);
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("status", 200);
        map.put("data", wxwordIPage.getRecords());
        map.put("count", wxwordIPage.getTotal());
        return map;
    }

}

